﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Task2
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义数据连接字符串
            string connString = "server=.;database=Game;Integrated Security=true";
            //创建连接对象
            SqlConnection conn = new SqlConnection(connString);
            //打开数据库连接
            conn.Open();
            //检测数据库连接是否打开
            if (conn.State==ConnectionState.Open)
            {
                //查询全部游戏
                //创建插入的SQL语句
                string sql = "select ID,Name,Price,Stock,XiaoJi from Games";
                SqlCommand cmd = new SqlCommand(sql,conn);
                //执行Sql语句
                SqlDataReader reader = cmd.ExecuteReader();
                Console.WriteLine("序号\t游戏名称\t游戏价格\t库存数量\t小计");
                Console.WriteLine("************************************************************");
                while (reader.Read())
                {
                    Console.WriteLine("{0}\t{1}\t{2:C}元\t\t{3}\t{4:C}元",
                        reader["ID"],
                        reader["Name"],
                        reader["Price"],
                        reader["Stock"],
                        reader["XiaoJi"]
                        );
                }
                reader.Close();
                Console.WriteLine("************************************************************");
                //统计金额
                string sqlMony = "select sum(Price*Stock) from Games";
                //创建Command对象（命令对象）
                cmd = new SqlCommand(sqlMony,conn);
                string sum = string.Format("{0:C}",cmd.ExecuteScalar());
                Console.WriteLine("游戏总金额：{0}元",sum);
            }
            //关闭数据连接
            conn.Close();
        }
    }
}
